#include "unitvector.hpp"
#include <iostream>
#include <vector>
using namespace std;
vector<UnitVector::Node> UL;


UnitVector::UnitVector(void)
{
	Busy=false;
}
UnitVector::~UnitVector(void)
{
}
//---------------------------------------------------------------------------
//进入临界区
//---------------------------------------------------------------------------
void UnitVector::EnterArea(void)
{
    while(Busy)
    {
        sleep(10);
    }
    Busy=true;
}
//---------------------------------------------------------------------------
//退出临界区
//---------------------------------------------------------------------------
void UnitVector::ExitArea(void)
{
    while(!Busy)
    {
        sleep(10);
    }
    Busy=false;
}
//---------------------------------------------------------------------------
//加入peer
//---------------------------------------------------------------------------
int UnitVector::Add(Node node)
{
	int r=0;
	r=SerchNode(node.Id);
	if(-1 == r)
	{
		EnterArea();
		UL.push_back(node);
		ExitArea();
		return	-1;
	}
	return r;
}
//---------------------------------------------------------------------------
//得到当前peer数
//---------------------------------------------------------------------------
int UnitVector::GetPeerNum()
{
    int num=0;

    EnterArea();
    num=UL.size();
    ExitArea();
    return num;
}
//---------------------------------------------------------------------------
//得到指定的peer信息
//---------------------------------------------------------------------------
bool UnitVector::GetPeerInfo(unsigned char *pId,Node *NTken)
{
	int idx=0;
	idx=SerchNode(pId);
	if(idx == -1)
	{
		return false;
	}
	else
	{
		EnterArea();
		memcpy(NTken,&UL[idx],sizeof(Node));
		ExitArea();
		return true;
	}
}
//---------------------------------------------------------------------------
//删除指定的peer信息
//---------------------------------------------------------------------------
bool UnitVector::DelNode(Node * NodInfo)
{
	int idx=0;
	vector<UnitVector::Node>::iterator Iter;

	idx=SerchNode(NodInfo->Id);
	if(idx == -1)
	{
		return false;
	}
	else
	{
		EnterArea();
		Iter = UL.begin();
		for(int i=0;i<idx;i++)
        {
            Iter++;
        }
		UL.erase(Iter);
		ExitArea();
		return true;
	}
}
//---------------------------------------------------------------------------
//搜索指定的节点
//---------------------------------------------------------------------------
int  UnitVector::SerchNode(unsigned char *pId)
{
	int num=0;
	int i=0;
	int len=sizeof(Node);
	int r=0;

	EnterArea();
	num=UL.size();
	ExitArea();
	for(i=0;i<num;i++)
	{
		r=memcmp(pId,UL[i].Id,6);
		if(r == 0)
		{//找到相同的ID号了
			break;
		}
	}

	if(i>=num)
	{
		return -1;
	}
	else
	{
		return i;
	}

}

bool UnitVector::GetPeerList(int idx,Node *Ntken)
{
	vector<UnitVector::Node>::iterator Iter;
	Node tmptk;
    EnterArea();
	Iter = UL.begin();
	for(int i=0;i<idx;i++)
    {
        Iter++;
    }
    tmptk = *Iter;
	memcpy(Ntken,&tmptk,sizeof(Node));
	ExitArea();
    return true;
}
#ifdef DEBUG
void UnitVector::PrintfNode(Node *idx)
{
//	int i=0;
	//显示ID号
	printf("ID:%x-%x-%x-%x-%x-%x\n",
		idx->Id[0],
		idx->Id[1],
		idx->Id[2],
		idx->Id[3],
		idx->Id[4],
		idx->Id[5]
		);
	//显示IP地址
	printf("IP:%s\n",inet_ntoa(idx->ClientAddr.sin_addr));
	//显示PORT

}
#endif


